<template>
  <!--  面包屑导航-->
  <div class="break">
    <el-breadcrumb separator=">">
      <el-breadcrumb-item>首页</el-breadcrumb-item>
      <el-breadcrumb-item>人事管理</el-breadcrumb-item>
      <el-breadcrumb-item>出境管理</el-breadcrumb-item>
      <el-breadcrumb-item><b>礼品领取</b></el-breadcrumb-item>
    </el-breadcrumb>
  </div>
  <!-- 搜索工具栏-->
  <div style="margin:40px 20px 20px 20px">
    <el-form :inline="true" ref="searchFormRef" :model="searchFormModel" class="demo-form-inline" style="margin-top: 20px">
      <el-form-item label="礼品名称" prop="giftname">
        <el-input v-model="searchFormModel.giftname" placeholder="请输入礼品名称" clearable />
      </el-form-item>
      <el-form-item label="礼品领取状态" prop="gift_claim_status">
        <el-input v-model="searchFormModel.gift_claim_status" placeholder="请输入礼品状态" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="success" @click="searchOnSubmit">查询</el-button>
        <!--        <el-button style="background-color: pink" @click="dialogAddState=true">添加</el-button>-->
      </el-form-item>
    </el-form>
  </div>
  <!--  数据表格-->
  <div style="margin:20px">
    <el-table :data="blogInfo" stripe border style="width: 100%"
    >
      <el-table-column prop="giftname" label="礼品名称" />
      <el-table-column prop="giftnumber" label="礼品数量"  />
      <el-table-column prop="gift_claim_status" label="礼品领取状态">
        <template #default="scope">
          <el-tag :type="statusType(scope.row.gift_claim_status)">
            {{ scope.row.gift_claim_status }}
            <span v-if="scope.row.gift_claim_status === '未领取'" class="status-indicator status-working"></span>
            <span v-if="scope.row.gift_claim_status === '已领取'" class="status-indicator status-holiday"></span>
          </el-tag>
        </template>
      </el-table-column>

      <el-table-column label="操作" width="160">
        <template #default="scope">
          <el-button
              size="small"
              type="danger"
              @click="tableDeleteHandler(scope.row.id)">
            <el-icon><Pointer /></el-icon>领取
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
  <!--  分页-->
  <div class="page">
    <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[3,5]"
        :small="small"
        :disabled="disabled"
        :background="background"
        layout="sizes, prev, pager, next"
        :total="totals"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
    />
  </div>
  <!-- 添加模态框 -->
  <el-dialog v-model="dialogAddState" title="添加礼品" width="680">
    <div>
      <el-form ref="formAddRef" :model="formAdd" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item prop="giftname" label="礼品名称">
              <el-input v-model="formAdd.giftname"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="giftnumber" label="礼品数量">
              <el-input v-model="formAdd.giftnumber"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogAddOnReset">取消</el-button>
        <el-button type="primary" @click="submitRegisterForm">确认</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import {Unlock,Lock,Delete,Edit,Pointer} from "@element-plus/icons";
import {onMounted, reactive, ref} from "vue";
import request from "@/utils/people";
import {ElMessage, ElMessageBox} from "element-plus";
onMounted(()=>{
  getGift();
})



const  blogInfo=ref([])
const statusType = (status) => {
  if (status === '未领取') {
    return 'success'; // 绿色
  } else if (status === '已领取') {
    return 'danger'; // 红色
  } else {
    return '';
  }
};

// 表格显示内容
const getGift = () => {
  const params = {
    pageNum: currentPage.value,
    pageSize: pageSize.value,
    // 初始时不包含ID参数
  };
  //模糊查询
  if (searchFormModel.giftname && searchFormModel.giftname.trim() !== '') {
    params.giftname = searchFormModel.giftname.trim();
  }
  if (searchFormModel.gift_claim_status && searchFormModel.gift_claim_status.trim() !== '') {
    params.gift_claim_status = searchFormModel.gift_claim_status.trim();
  }
  request({
    url: '/gift/getAll',
    method: 'get',
    params,
  }).then((req) => {
    console.log(req)
    if (200 == req.data.errorCode) {
      blogInfo.value = req.data.data.records.map(item => ({
        ...item,
      }));
      totals.value = req.data.data.total;

    }
  })
}

//分页
const currentPage = ref(1)
const pageSize = ref(5)
const small = ref(false)
const background = ref(false)
const disabled = ref(false)
let totals=ref(0);
const handleSizeChange = (val) => {
  console.log(`${val} items per page`)
  pageSize.value=val;
  getGift()
}
const handleCurrentChange = (val) => {
  console.log(`current page: ${val}`)
  currentPage.value=val;
  getGift()
}
//搜索
const searchFormRef=ref()
const searchFormModel=reactive({
  id:'',
  giftname:'',
  giftnumber:'',
  gift_claim_status:'',
  status:'',
})
const searchOnSubmit=()=>{
  getGift()
}



///////////////////添加/////////////////////////
const dialogAddState = ref(false);
const formAddData = {
  id:'',
  information_id:'',
  giftname:'',
  giftnumber:'',
  gift_claim_status:'',
  status:'',
};
const formAdd = reactive({...formAddData});
const addressFormRef = ref(null); // 创建一个ref引用form
// 提交表单
const submitRegisterForm = ()=>{
  request({
    method:'post',
    url:'/gift/insert',
    params:formAdd
  }).then(req=>{
    ElMessage({
      type:'info',
      message:req.data.msg
    })
    if (200 == req.data.errorCode){
      Object.assign(formAdd,formAddData)
      getGift()
    }
  })
  dialogAddState.value=false;
}
const formEdit = reactive({...formAddData})
const tableEditHandler = (rowIndex , rowData) => {
  console.log("-------------")
  console.log(rowIndex)
  console.log(rowData)

  formEdit.id = rowData.id
  formEdit.giftname= rowData.giftname
  formEdit.giftnumber = rowData.giftnumber
  formEdit.gift_claim_status=rowData.gift_claim_status
  dialogEditState.value = true
}
const dialogEditState = ref(false)
//撤销
const tableDeleteHandler = (id) => {
  console.log(id)
  request({
    url: `/gift/updateS?id=${id}`, // 如果后端通过 URL 参数接收 ID
    method: 'put',
    // 如果后端期望 ID 在请求体中，可以这样设置
    data: JSON.stringify({...formEdit, id }),
    headers: {'content-type': "application/json;charset=UTF-8"}
  }).then(req => {
    ElMessage({
      type: 'info',
      message: req.data.msg
    });
    if (200 === req.data.errorCode) {
      getGift();
    }
  });
  dialogEditState.value = false;
};
</script>

<style scoped>
.page{
  margin-left: 40%;
}
.el-breadcrumb{
  font-size: 18px;
}
.break{
  margin: 5px 0 0 10px;
}
.status-indicator {
  display: inline-block;
  width: 10px;
  height: 5px;
  border-radius: 50%;
  margin-left: 5px;
  vertical-align: middle;
  position: relative;
  top: -3px;
  background-color: transparent !important; /* 防止el-tag背景色影响 */
}

.status-working {
  background-color: green;
}

.status-holiday {
  background-color: red;
}
</style>